package com.example.his.api.front.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.hutool.core.bean.BeanUtil;
import com.example.his.api.common.PageUtils;
import com.example.his.api.common.R;
import com.example.his.api.config.sa_token.StpCustomerUtil;
import com.example.his.api.front.controller.form.SearchGoodsListByPageForm;
import com.example.his.api.front.controller.form.SearchIndexGoodsByPartForm;
import com.example.his.api.front.service.GoodsService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Map;

/**
 * @author huixh
 */
@RequestMapping("/front/goods")
@RestController("FrontGoodsController")
public class GoodsController {
    @Resource
    private GoodsService goodsService;

    @PostMapping("/searchIndexGoodsByPart")
    public R searchIndexGoodsByPart(@RequestBody @Valid SearchIndexGoodsByPartForm form){
        Map<String, Object> map = goodsService.searchIndexGoodsByPartId(form.getPartIds());
        return R.ok().put("result", map);
    }

    @PostMapping("/searchListByPage")
    public R searchListByPage(@RequestBody @Valid SearchGoodsListByPageForm form){
        Integer page = form.getPage();
        Integer length = form.getLength();
        int start = (page - 1) * length;
        Map<String, Object> map = BeanUtil.beanToMap(form);
        map.put("start",start);
        PageUtils pageUtils = goodsService.searchListByPage(map);
        return R.ok().put("page",pageUtils);
    }
}
